package com.android.takegism.ui;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.android.takegism.R;
import com.android.takegism.service.AdminService;
import com.android.takegism.utils.Clue;
import com.android.takegism.utils.SensorPackage;
import com.android.takegism.utils.SensorUiItem;

import java.util.Set;

public class ZzjTestActivity extends AppCompatActivity implements View.OnClickListener {

    Button button_zzj_test_1;
    Button send_msg_btn;

    EditText data_type_text;
    EditText data_string_text;

    EditText key_text_1;
    EditText key_text_2;
    EditText key_text_3;

    EditText val_text_1;
    EditText val_text_2;
    EditText val_text_3;

    Button send_test_package;
    Button select_data_type;
    Button test_camera_btn;

    EditText send_msg_text;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zzj_test);
        button_zzj_test_1 = findViewById(R.id.button_zzj_test_1);
        send_msg_btn = findViewById(R.id.send_msg_btn);
        data_type_text = findViewById(R.id.data_type_text);
        data_string_text = findViewById(R.id.data_string_text);

        send_msg_text = findViewById(R.id.send_msg_text);

        key_text_1 = findViewById(R.id.key_text_1);
        key_text_2 = findViewById(R.id.key_text_2);
        key_text_3 = findViewById(R.id.key_text_3);

        val_text_1 = findViewById(R.id.val_text_1);
        val_text_2 = findViewById(R.id.val_text_2);
        val_text_3 = findViewById(R.id.val_text_3);

        send_test_package = findViewById(R.id.send_test_package);
        select_data_type = findViewById(R.id.select_data_type);
        test_camera_btn = findViewById(R.id.test_camera_btn);


        button_zzj_test_1.setOnClickListener(this);
        send_test_package.setOnClickListener(this);
        select_data_type.setOnClickListener(this);
        send_msg_btn.setOnClickListener(this);

        test_camera_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(ZzjTestActivity.this,TestCameraActivity.class));
            }
        });
    }

    @Override
    public void onClick(View v) {
        AlertDialog.Builder builder = new AlertDialog.Builder(ZzjTestActivity.this);
        AlertDialog.Builder builder2 = new AlertDialog.Builder(ZzjTestActivity.this);
        Set<String> strings = SensorUiItem.getString2ImageIdMap().keySet();
        String[] strs = new String[strings.size()];
        strings.toArray(strs);
        builder.setTitle("数据类型必须为以下之一")
//                                .setMessage("")
                .setSingleChoiceItems(strs, 0, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        data_type_text.setText(strs[which]);
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ;
                    }
                })
                .setNegativeButton("我自己输入", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        System.out.println("ok");
                    }
                });

        switch (v.getId()) {
            case R.id.send_msg_btn:
//                AdminService adminService = (AdminService) MyContext.getInstance().getStatics("adminService");
//                adminService.sendMessage(send_msg_text.getText().toString());
                Intent intent0 = new Intent(ZzjTestActivity.this, AdminService.class);
                Bundle bundle0 = new Bundle();
                bundle0.putSerializable("clue", new Clue(0, "广播线索" + send_msg_text.getText().toString()));
                bundle0.putSerializable("type", "线索clue");
                intent0.putExtras(bundle0);
                System.out.println("测试广播服务。。。");
                startService(intent0);
                break;
            case R.id.button_zzj_test_1:
                Intent intent = new Intent(ZzjTestActivity.this, AdminService.class);
                Bundle bundle = new Bundle();
                SensorPackage sensorPackage = new SensorPackage("声音传感器");
                sensorPackage.put("string", "检测到声音");
                bundle.putSerializable("sensorPackage", sensorPackage);
                intent.putExtras(bundle);
                System.out.println("来了");
                startService(intent);
                break;
            case R.id.select_data_type:
                builder.create().show();
                break;
            case R.id.send_test_package:
                String data_type_string = data_type_text.getText().toString();
                String data_string_string = data_string_text.getText().toString();
                if (data_type_string == null || data_type_string.equals("")) {
                    builder.setTitle("提示")
                            .setMessage("数据类型不可以为空")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    ;
                                }
                            });
                    builder.create().show();
                    break;
                }
                if (!SensorUiItem.getString2ImageIdMap().containsKey(data_type_string)) {
                    builder.create().show();
                    break;
                } else {
                    System.out.println("ok");
                }
                if (data_string_string == null || data_string_string.equals("")) {
                    builder.setTitle("提示");
                    builder.setMessage("数据字符串不可以为空");
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            ;
                        }
                    });
                    builder.create().show();
                    break;
                }
                SensorPackage sp = new SensorPackage();
                sp.setDataType(data_type_string);
                sp.setDataString(data_string_string);
                String key_text_1_str = key_text_1.getText().toString();
                String key_text_2_str = key_text_2.getText().toString();
                String key_text_3_str = key_text_3.getText().toString();

                Object val_text_1_str = val_text_1.getText().toString();
                Object val_text_2_str = val_text_2.getText().toString();
                Object val_text_3_str = val_text_3.getText().toString();

                sp.put(key_text_1_str, val_text_1_str);
                sp.put(key_text_2_str, val_text_2_str);
                sp.put(key_text_3_str, val_text_3_str);

                StringBuffer sb = new StringBuffer();
                sb.append("数据类型：")
                        .append(data_type_string)
                        .append("\n数据字符串：")
                        .append(data_string_string)
                        .append("\n数据包：")
                        .append(sp.getData().toString());

                AlertDialog.Builder builder3 = new AlertDialog.Builder(ZzjTestActivity.this);
                builder3.setTitle("发送数据包")
                        .setMessage(sb.toString());
                builder3.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (SensorUiItem.getString2ImageIdMap().containsKey(data_type_string)) {
                            Intent intent = new Intent(ZzjTestActivity.this, AdminService.class);
                            Bundle bundle = new Bundle();
                            bundle.putSerializable("sensorPackage", sp);
                            intent.putExtras(bundle);
                            System.out.println("测试服务。。。");
                            startService(intent);
                        }else{
                            builder.create().show();
                        }
                    }
                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ;
                    }
                });
                builder3.create().show();
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + v.getId());
        }
    }
}